package com.jiyun.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.mapper.OrdersMapper;
import com.jiyun.mapper.RiderQueueMapper;
import com.jiyun.pojo.Orders;
import com.jiyun.pojo.RiderQueue;
import com.jiyun.service.IOrdersService;
import com.jiyun.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private RiderQueueMapper riderQueueMapper;

    @Override
    public Result findOrders(String oid, Integer id) {
        if (id == null) {
            return Result.fail("findOrders方法传入的id参数为null，无法进行查询操作");
        }
        System.out.println("id===" + id);
        System.out.println("oid===" + oid);

        LambdaQueryWrapper<RiderQueue> qw = new LambdaQueryWrapper<>();
        qw.eq(RiderQueue::getId, id);
        RiderQueue riderQueue = riderQueueMapper.selectOne(qw);
        System.out.println("riderQueue=============== " + riderQueue);

        if (riderQueue == null) {
            return Result.fail("findOrders方法未找到对应状态为5的RiderQueue记录");
        }

        Orders orders1 = ordersMapper.selectOrdersByOid(oid);

        orders1.setODrivername(riderQueue.getDid());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String endTimeStr = sdf.format(riderQueue.getEndTime());
        orders1.setPayarrivetime(DateUtil.parse(endTimeStr));

        orders1.setMileage(riderQueue.getDistance());

        orders1.setOrderstatus(1);

        orders1.setDriverevaluation(riderQueue.getDrivervaluation());//司机评价

        orders1.setCarId(riderQueue.getDid());

        if (riderQueue.getCityTime() == null) {
            orders1.setPayarrivetime(new Date());
        }
        String getCityTime = sdf.format(riderQueue.getCityTime());

        orders1.setPayarrivetime(DateUtil.parse(getCityTime));

        orders1.setPassengerstime(riderQueue.getCityTime());

        Date date1 = riderQueue.getCityTime();
        Date date2 = riderQueue.getEndTime();
        long minutes = DateUtil.betweenMonth(date1, date2, false);
        orders1.setPayconsuming((int) minutes);

        System.out.println("orders1======" + orders1);
        ordersMapper.updateById(orders1);

        // 时间值非空判断，若时间值为空则返回null，提示数据异常
        if (date1 == null || date2 == null) {
            return Result.fail("RiderQueue中的时间值存在null，无法计算时间差，数据可能异常，请检查数据完整性。");
        }
        // 数据类型转换健壮性处理，判断分钟数是否超出Integer范围，若超出则返回null并记录日志
        if (minutes > Integer.MAX_VALUE || minutes < Integer.MIN_VALUE) {
            return Result.fail("计算出的时间差分钟数超出了Integer类型的表示范围，无法进行转换设置。");
        }
        return Result.ok(orders1);
    }

}
